package com.jack.leetcode.每日一题;

import javax.xml.stream.events.Characters;

/***
 * @author zhangjie
 * @date 2020/4/3 19:17
 */
public class 字符串转换整数 {

    public static void main(String[] args) {

    }

    public static int myAtoi(String str) {
        int result = 0;
        str = str.trim();
        int n = str.length();
        if (n == 0 || Character.isLetter(str.charAt(0))) {
            return result;
        }
        boolean nevigate = false;
        int idx = 0;
        if (str.charAt(0) == '+') {
            idx++;
        } else if (str.charAt(0) == '-') {
            idx++;
            nevigate = true;
        }
        while (idx < n && Character.isDigit(str.charAt(idx))) {
            int digit = str.charAt(idx) - '0';
            if (result > (Integer.MAX_VALUE - digit) / 10) {
                return nevigate ? Integer.MIN_VALUE : Integer.MAX_VALUE;
            }
            result = result*10 +digit;
            idx++;
        }
        return nevigate?-result:result;
    }
}
